new Vue({
    el: '#app',
    data() {
        return {
            form: {
                Table_Name: '',//表名
                isChildrenTable: false,//是否为子表
                Parent_Table_Name: '',
                Parent_Primary_Key: '',
                Foreign_Key: '',
                index: '',
                Children: {
                    base_table_detail: [{
                        Table_Detail_Name: '',//名称
                        Table_Detail_Label: '',//备注
                        Table_Detail_Type: 'varchar',//类型
                        Table_Detail_Length: 50,//长度
                        Table_Detail_Default: '',//默认值
                        Table_Detail_Index: '',//索引
                        isNull: false,
                        isPrimaryKey: true,
                    }],
                }
            },
            Table: [],//所有表名
            Column: [],//字段
            rules: {
                Table_Name: [{ required: true, message: '请输入表名称', trigger: 'blur' }],
                Table_Detail_Name: [{ required: true, message: '请输入字段名称', trigger: 'blur' }],
                Foreign_Key: [{ required: true, message: '请选择被栏位', trigger: 'change' }],
                Parent_Table_Name: [{ required: true, message: '请选择被参考表', trigger: 'change' }],
                Parent_Primary_Key: [{ required: true, message: '请选择参考栏位', trigger: 'change' }],
                index: [{ required: true, message: '请输入名', trigger: 'blur' }],
            },
            Type: ['varchar', 'text'],
            loading: false,
        }
    },
    mounted() {
        this.getData();
    },
    methods: {
        getData() {
            const that = this
            $.ajax({
                headers:requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryAllTableName`,
                type: 'GET',
                success: function (data) {
                    if (data.length > 0) {
                        that.Table = data
                    }
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            })
        },
        submitForm(formName) {
            const that = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let obj = this.generateCreateSQL(this.form)
                    if (obj.Status) {
                        that.loading = true;
                        $.ajax({
                            headers:requestHeaders(),
                            url: `${BASE_URL2}/generalBusiness/generalDataTableOperation`,
                            type: 'POST',
                            data: {
                                Data: obj.SQL
                            },
                            success: function (res) {
                                if (res.Status === 'success') {
                                    that.$message.success("提交成功")
                                    window.location.href='./index.html'
                                } else {
                                    that.$message.error("提交失败，请稍后再试")
                                }
                            },
                            complete: function (XMLHttpRequest) {
                                that.loading = false
                                requestPretreatment(XMLHttpRequest);
                            },
                            dataType: "json"
                        })
                    } else {
                        this.$message({
                            message: '请选择主键！',
                            type: 'warning'
                        })
                        return false;
                    }
                } else {
                    that.$message({
                        message: sumberError,
                        type: 'warning'
                    })
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        generateCreateSQL(obj) {//生成sql
            if (obj.Children.base_table_detail.some(item => item.isPrimaryKey && item.Table_Detail_Type == 'varchar')) {
                //表名
                let SQL = "CREATE TABLE `" + obj.Table_Name + "` (\r\n";
                //字段
                obj.Children.base_table_detail.forEach(item => {
                    let Table_Detail_Type = item.Table_Detail_Type;
                    let DEFAULT = "DEFAULT '" + item.Table_Detail_Default + "'";
                    if (Table_Detail_Type == 'varchar') {
                        Table_Detail_Type = Table_Detail_Type + "(" + item.Table_Detail_Length + ")";
                    } else {//text 不能有默认值
                        DEFAULT = "";
                    }
                    SQL = SQL + "`" + item.Table_Detail_Name + "` " + Table_Detail_Type + (item.isNull ? '' : ' NOT') + " NULL " + DEFAULT + " COMMENT '" + item.Table_Detail_Label + "',\r\n";
                });
                //主键
                SQL = SQL + "PRIMARY KEY (";
                obj.Children.base_table_detail.filter(item => item.isPrimaryKey && item.Table_Detail_Type == 'varchar').forEach((item, index) => {
                    if (index == 0) {
                        SQL = SQL + "`" + item.Table_Detail_Name + "`"
                    } else {
                        SQL = SQL + ", `" + item.Table_Detail_Name + "`"
                    }
                })
                SQL = SQL + ")";
                //外键
                if (obj.isChildrenTable) {
                    SQL = SQL + ",\r\nCONSTRAINT `" + obj.index + "` FOREIGN KEY (`" + obj.Foreign_Key + "`) REFERENCES `" + obj.Parent_Table_Name + "` (`" + obj.Parent_Primary_Key + "`)"
                }
                //索引
                obj.Children.base_table_detail.filter(item => item.Table_Detail_Index && item.Table_Detail_Type == 'varchar').forEach(item => {
                    SQL = SQL + ",\r\nINDEX `" + item.Table_Detail_Index + "` (`" + item.Table_Detail_Name + "`)";
                })
                SQL = SQL + "\r\n);";
                return { Status: true, SQL }
            } else {
                return { Status: false }
            }
        },
        base_table_detail_add() {//添加
            this.form.Children.base_table_detail.push({
                Table_Detail_Name: '',//名称
                Table_Detail_Label: '',//备注
                Table_Detail_Type: 'varchar',//类型
                Table_Detail_Length: 50,//长度
                Table_Detail_Default: '',//默认值
                isNull: false,
                Primary_Key: false
            })
        },
        base_table_detail_remove() {//移除
            if (this.form.Children.base_table_detail.length > 1) {
                this.form.Children.base_table_detail.pop();
            } else {
                this.$message({
                    message: '不能在删除了',
                    type: 'warning'
                })
            }
        },
        ChangeType(e, item) {//更新字段类型
            if (e == 'text') {
                item.Table_Detail_Length = 0;//长度默认0
                item.isPrimaryKey = false;//默认没有主键
                item.Table_Detail_Default = '';//默认没有默认值
                item.Table_Detail_Index = '';//默认没有索引
            } else if (e == 'varchar') {
                item.Table_Detail_Length = 50;//长度默认0
            }
        },
        ChangeTable(Data) {//选择表查询字段
            const that = this
            this.Column = [];//初始化
            $.ajax({
                headers:requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryAllColumnByTableName`,
                type: 'GET',
                data: { Data },
                success: function (data) {
                    console.log(data);
                    if (data.length > 0) {
                        that.Column = data;
                    }
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false;
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            })
        }
    }
})